package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.service.IZyconsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Zyrecharge;
import com.ruoyi.system.mapper.ZyconsumeMapper;
import com.ruoyi.system.domain.Zyconsume;

/**
 * 住院消费记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@Service
public class ZyconsumeServiceImpl implements IZyconsumeService
{
    @Autowired
    private ZyconsumeMapper zyconsumeMapper;

    /**
     * 查询住院消费记录
     * 
     * @param zyid 住院消费记录主键
     * @return 住院消费记录
     */
    @Override
    public Zyconsume selectZyconsumeByZyid(Long zyid)
    {
        return zyconsumeMapper.selectZyconsumeByZyid(zyid);
    }

    /**
     * 查询住院消费记录列表
     * 
     * @param zyconsume 住院消费记录
     * @return 住院消费记录
     */
    @Override
    public List<Zyconsume> selectZyconsumeList(Zyconsume zyconsume)
    {
        return zyconsumeMapper.selectZyconsumeList(zyconsume);
    }

    /**
     * 新增住院消费记录
     * 
     * @param zyconsume 住院消费记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZyconsume(Zyconsume zyconsume)
    {
        int rows = zyconsumeMapper.insertZyconsume(zyconsume);
        insertZyrecharge(zyconsume);
        return rows;
    }

    /**
     * 修改住院消费记录
     * 
     * @param zyconsume 住院消费记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZyconsume(Zyconsume zyconsume)
    {
        zyconsumeMapper.deleteZyrechargeByZyid(zyconsume.getZyid());
        insertZyrecharge(zyconsume);
        return zyconsumeMapper.updateZyconsume(zyconsume);
    }

    /**
     * 批量删除住院消费记录
     * 
     * @param ids 需要删除的住院消费记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZyconsumeByZyids(Long[] ids)
    {
        zyconsumeMapper.deleteZyrechargeByZyids(ids);
        return zyconsumeMapper.deleteZyconsumeByZyids(ids);
    }

    /**
     * 删除住院消费记录信息
     * 
     * @param zyid 住院消费记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZyconsumeByZyid(Long zyid)
    {
        zyconsumeMapper.deleteZyrechargeByZyid(zyid);
        return zyconsumeMapper.deleteZyconsumeByZyid(zyid);
    }

    /**
     * 新增住院充值记录信息
     * 
     * @param zyconsume 住院消费记录对象
     */
    public void insertZyrecharge(Zyconsume zyconsume)
    {
        List<Zyrecharge> zyrechargeList = zyconsume.getZyrechargeList();
        Long zyid = zyconsume.getZyid();
        if (StringUtils.isNotNull(zyrechargeList))
        {
            List<Zyrecharge> list = new ArrayList<Zyrecharge>();
            for (Zyrecharge zyrecharge : zyrechargeList)
            {
                zyrecharge.setZyid(zyid);
                list.add(zyrecharge);
            }
            if (list.size() > 0)
            {
                zyconsumeMapper.batchZyrecharge(list);
            }
        }
    }
}
